package com.liuyang.demo.optional;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Slf4j
public class OptionalDemo {
    public static void main(String[] args) {

        //method(null);
        integerDemo(Integer.MAX_VALUE+1);
    }
    public static void method(Role role) {
        // 当Optional的值通过常量获得或者通过关键字new初始化，可以直接使用of()方法
        Optional<String> strOpt = Optional.of("Hello World");
        Optional<Role> roleOpt1 = Optional.of(new Role());

        // 方法参数中role值不确定是否为null，使用ofNullable()方法创建
        Optional<Role> roleOpt2 = Optional.ofNullable(role);

        log.info(strOpt.orElse("Hello World"));

        User user = null;
        Optional<User> userOpt = Optional.ofNullable(user);

        log.info(userOpt.map(User::getName)
                .map(String::toUpperCase)
                .orElse(null));
    }

    public static void integerDemo(Object s){

        Optional<Object> s1 = Optional.ofNullable(s);
        System.out.println(s1.isPresent());

        if(s1.isPresent()||s1.get().equals(1)){
            System.out.println("------------");
        }
    }
}
